package org.pewpew.client;

import java.util.ArrayList;
import java.util.List;

import org.pewpew.model.Command;
import org.pewpew.model.Player;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

public class ControllerPollerService extends Service implements UICommunication { 
	private final Messenger inboundMessenger = new Messenger(new IncomingHandler());
	private List<Messenger> outboundListeners = new ArrayList<Messenger>(); 
	
	private HttpPoller poller = null;
	private Hardware hardware = new Hardware (); 
	
	@Override
	public IBinder onBind(Intent intent) {
		return inboundMessenger.getBinder();
	}

	class IncomingHandler extends Handler { 
		@Override
		public void handleMessage(Message msg) {
			String commandJSON = null;
			
			switch (msg.what) {
			case Messaging.MSG_REGISTER_CLIENT:
				Log.i ("PEW-service", "Register client arrived");
				outboundListeners.add(msg.replyTo);
				commandJSON = msg.getData().getString(Messaging.COMMAND);
				break;
			case Messaging.MSG_UNREGISTER_CLIENT:
				Log.i ("PEW-service", "Unregister client arrived");
				outboundListeners.remove(msg.replyTo);
				break;
			case Messaging.MSG_PROCESS_CLIENT_COMMAND:
				Log.i ("PEW-service", "Process client command arrived");
				commandJSON = msg.getData().getString(Messaging.COMMAND);
				break;
			default:
				super.handleMessage(msg);
			}
			
			if (commandJSON != null) {
				processClientCommand (Command.fromJSON(commandJSON));
			}
		}

	}

	private void processClientCommand(Command command) {
		Log.i("PEW-service", "Processing client command: " + command.toJSON());
		if (command.getAction().equals (Messaging.CMD_SET_USERNAME)) {
			hardware.setUsername(command.getObject());
			
		} else if (command.getAction().equals (Messaging.CMD_SHOOT)) {
			poller.shoot (command);
		}
		else {
			Log.w ("PEW-service", "Unknown command: " + command.toJSON());
		}
	}
	
	@Override
	public void onCreate() {
		super.onCreate();

		hardware.setUpOrientationListener(this);
		hardware.setUpGPSListener(this);
		
		poller = new HttpPoller (this);
		poller.setupPollerThread();
	}

	@Override
	public void onDestroy() {
		poller.stop ();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i("PEW-service", "Received start id " + startId + ": " + intent);
		return START_NOT_STICKY; 
	}

	public void sendWorldStateUI(String string) {
		Intent intent = new Intent(Messaging.NEW_MESSAGE);
		intent.putExtra(Messaging.MESSAGE, string);
		sendBroadcast(intent);
	}
	
	//is not working for messengers
	public void sendWorldStateUI2(String worldState) {
		if (worldState == null) {
			Log.w ("PEW-service", "Receieved null world state string");
			return;
		}
		for (int i = outboundListeners.size() - 1; i >= 0; i--) {
			try {
				Log.i ("PEW-service", "Sending to GUI" + worldState);
				Message msg = Messaging.packToMessage(Messaging.MSG_UPDATE_WORLD_STATE, worldState);
				outboundListeners.get(i).send(msg);

			} catch (RemoteException e) {
				//we can do it as we go from size() to 0
				outboundListeners.remove(i);
			}
		}
	}

	public Player getPlayer() {
		return hardware.getPlayer ();
	}

	public boolean isReady() {
		return hardware.isReady();
	}

}
